

const gulp = require('gulp');
const template = require("gulp-art-template4");
const htmlmin = require('gulp-htmlmin');
const htmlBeautify = require('gulp-html-beautify');
const concat = require('gulp-concat');
const sass = require('gulp-sass');
const autoprefixer = require('gulp-autoprefixer');
const cssmin = require('gulp-clean-css');
const uglify = require('gulp-uglify');
const cssbeautify = require('gulp-cssbeautify');
const babel = require('gulp-babel');
const webserver = require('gulp-webserver');
const del = require('del');
const config = require('./config.js');


//本地版本不支持

//const project_name = config.project_name;
const project_name = 'home';


const htmlWatchReg = './src/' + project_name + '/**/*.art';
const htmlReg = './src/' + project_name + '/views/**/*.art';
//模板渲染 art-template
const renderTask = function () {
  var options = {
    removeComments: true,//清除HTML注释  <!--! 保留注释 -->
    collapseWhitespace: true,//压缩HTML 成一行
    collapseBooleanAttributes: true,//省略布尔属性的值 <input checked="true"/> ==> <input />
    removeEmptyAttributes: true,//删除所有空格作属性值 <input id="" /> ==> <input />
    // removeScriptTypeAttributes: true,//删除<script>的type="text/javascript"
    // removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
    // minifyJS: true,//压缩页面JS
    // minifyCSS: true//压缩页面CSS
  };
  return gulp.src(htmlReg)
    .pipe(template({
      t_global: {}//规定模板全局变量 t_global
    }, {
      extname: '.art' // some art-template options
    }, {
      ext: '.html'
    }))
    .pipe(htmlmin(options))
    .pipe(htmlBeautify({
      indent_size: 4,
      indent_char: ' ',
      // 这里是关键，可以让一个标签独占一行
      unformatted: true,
      // 默认情况下，body | head 标签前会有一行空格
      extra_liners: []
    }))
    .pipe(gulp.dest("./dist/" + project_name+'/pages/'))
}

//处理scss 
//所有项目公共css 项目公共合并成public.css 
//项目页面合并style.css
const scssPublicWatchReg = ['./src/css/**/*.scss','./src/' + project_name + '/css/**/*.scss','./src/' + project_name + '/layout/**/*.scss'];
const scssPublicReg = ['./src/' + project_name + '/css/**/*.scss','./src/' + project_name + '/layout/**/*.scss'];
const cssPublicTask = function () {
   return gulp.src(scssPublicReg)
    .pipe(sass({
      precision:10 //精度10
    }).on('error', sass.logError)) //sass处理
    .pipe(autoprefixer())
    .pipe(concat('public.css'))//合并到一个文件
    .pipe(cssmin())
    .pipe(cssbeautify({
      indent: '  ',
      openbrace: 'separate-line',
      autosemicolon: true
    }))
    .pipe(gulp.dest('./dist/'+project_name+'/css'));
}

const scssReg = ['./src/' + project_name + '/views/**/*.scss'];
const cssTask = function () {
  return gulp.src(scssReg)
    .pipe(sass({
      precision:10 //精度10
    }).on('error', sass.logError))
    .pipe(autoprefixer())
    .pipe(concat('style.css'))
    .pipe(cssmin())
    .pipe(cssbeautify({
      indent: '  ',
      openbrace: 'separate-line',
      autosemicolon: true
    }))
    .pipe(gulp.dest('./dist/'+project_name+'/css'));
}

//处理js 合并成public.js
const jsPublicReg = ['./src/js/**/*.js','./src/' + project_name + '/js/**/*.js'];
const jsPublicTask = function () {
  return gulp.src(jsPublicReg)
    .pipe(babel({
        presets: ['@babel/env']
    }))//转es5
    .pipe(concat('public.js'))//合并到一个文件
    .pipe(uglify())
    .pipe(gulp.dest('./dist/'+project_name+'/js'));
}

//图片处理 如压缩不做
const imgReg = ['./src/' + project_name + '/images/**/*.*'];
const imageTask = function () {
  return gulp.src(imgReg)
    .pipe(gulp.dest('./dist/'+project_name+'/images'));
}

//第三方lib 插件
const pluginReg = ['./src/plugin/*.*'];
const pluginTask = function () {
  return gulp.src(pluginReg).pipe(gulp.dest("./dist/"+project_name+"/plugin"))
}

//启动服务
const webTask = function(){
  return gulp.src('./dist/'+project_name)
          .pipe(webserver({
            host:'localhost',
            port:'8066',
            livereload:true,//文件修改自动刷新
            open:'./pages/index.html', //默认页
            proxies:[ //路径识别代理 开发测试跨域 
              {
                source:'/baidu',
                target:'https://www.baidu.com/'
              }
            ]
          }))
}

//watch 监控刷新
const watchTask = function(){
  gulp.watch(htmlWatchReg,renderTask);
  gulp.watch(scssPublicWatchReg,cssPublicTask);
  gulp.watch(scssReg,cssTask);
  gulp.watch(jsPublicReg,jsPublicTask);
  gulp.watch(imgReg,imageTask);
  gulp.watch(pluginReg,pluginTask);
}

//清除dist  多余文件建议手动
const delTask = function(){
  return del(['./dist/'+project_name]);
}

module.exports.clean = delTask;
module.exports.pack = gulp.parallel(renderTask,cssPublicTask,cssTask,jsPublicTask, imageTask,pluginTask);

module.exports.default = gulp.series(
  gulp.parallel(renderTask,cssPublicTask,cssTask,jsPublicTask, imageTask,pluginTask),
  webTask,
  watchTask
);